#!/usr/bin/python

#Copyright David Zaragoza, 2012

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sys import argv
from subprocess import getoutput

base_directory = '.'

def install(packages):
	global base_directory
	getoutput('cat '+base_directory+'/exception | tomoyo-loadpolicy -e;tomoyo-savepolicy')
	
	if packages == ['all']:
		getoutput('cat '+base_directory+'/policy/* | tomoyo-loadpolicy -d;tomoyo-savepolicy')
	else:
		for p in packages:
			a = getoutput('cat '+base_directory+'/policy/'+p+' | tomoyo-loadpolicy -d;tomoyo-savepolicy')
			print(a)
		
def list():
	print(getoutput('ls '+base_directory+'/policy'))
	
def remove(packages):
	global base_directory
	if packages == ['all']:
		getoutput('cat '+base_directory+'/remove/* | tomoyo-loadpolicy -d;tomoyo-savepolicy')
	else:
		for p in packages:
			getoutput('cat '+base_directory+'/remove/'+p+' | tomoyo-loadpolicy -d;tomoyo-savepolicy')

def help():
	print(getoutput('cat '+base_directory+'/help'))
	exit()

if len(argv) < 2:
	help()

if argv[1] == 'install':
	if len(argv) < 3:
		help()
	install(argv[2:])
elif argv[1] == 'help':
	help()
elif argv[1] == 'list':
	list()
elif argv[1] == 'remove':
	if len(argv) < 3:
		help()
	remove(argv[2:])
else:
	help()
